<template>
  <div class="app-container">
    <el-col>
      <el-col :span="4">
        <div class="grid-content bg-purple">
          <el-tree
            :data="categories"
            :props="defaultProps"
            node-key="id"
            default-expand-all
            :expand-on-click-node="false"
            style="width:60%"
            @node-click="nodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20">
        <div class="grid-content bg-purple-light">
          <el-button type="primary" @click="add">
            <i class="el-icon-upload el-icon-plus" />
            新增
          </el-button>
          <el-table :data="contents" border style="width: 100%;">
            <el-table-column prop="id" label="内容编号" width="80" />
            <el-table-column prop="title" label="内容标题" />
            <el-table-column prop="subTitle" label="内容子标题" />
            <el-table-column prop="titleDesc" label="内容标题描述" />
            <el-table-column prop="url" label="链接" />
            <el-table-column label="图片">
              <template slot-scope="scope">
                <img :src="scope.row.pic" height="60px">
                <img :src="scope.row.pic2" height="60px">
              </template>
            </el-table-column>
            <el-table-column prop="content" label="内容详情" width="80" />
            <el-table-column prop="created" label="创建时间" width="120" />
            <el-table-column prop="updated" label="更新时间" width="120" />

            <el-table-column fixed="right" label="操作" width="100">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="handleClick(scope.row)">编辑</el-button>
                <el-button type="text" size="small" @click="deleteClick(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-col>

    <!-- 编辑商品对话框 -->
    <el-dialog title="编辑内容" :visible.sync="dialogVisible" width="80%" top="0px">
      <el-form ref="form" :model="content" label-width="80px">
        <el-form-item label="内容标题">
          <el-input v-model="content.title" />
        </el-form-item>
        <el-form-item label="内容子标题">
          <el-input v-model="content.subTitle" />
        </el-form-item>
        <el-form-item label="内容标题描述">
          <el-input v-model="content.titleDesc" type="textarea" />
        </el-form-item>
        <el-form-item label="内容链接">
          <el-input v-model="content.url" />
        </el-form-item>
        <el-form-item label="商品图片1">
          <el-upload
            action="/api/file/upload/item"
            list-type="picture-card"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :limit="this.limit"
            :on-exceed="handleExceed"
            :on-success="handleSuccess"
          >
            <i class="el-icon-plus" />
          </el-upload>
        </el-form-item>
        <el-form-item label="商品图片2(选填)">
          <el-upload
            action="/api/file/upload/item"
            list-type="picture-card"
            :on-preview="handlePreview2"
            :on-remove="handleRemove2"
            :on-exceed="handleExceed"
            :on-success="handleSuccess2"
          >
            <i class="el-icon-plus" />
          </el-upload>
        </el-form-item>
        <el-form-item label="内容分类">
          <el-button size="small" type="primary" @click="categoryDialogVisible = true">选择类目</el-button>
          <span v-text="this.cname" />
        </el-form-item>

        <el-form-item label="内容详情描述">
          <el-input v-model="content.content" type="textarea" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit">保存</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 图片预览dialog -->
    <el-dialog :visible.sync="previewDialogVisible">
      <img width="100%" :src="dialogImageUrl" alt>
    </el-dialog>
    <!-- 选择分类dialog -->
    <el-dialog :visible.sync="categoryDialogVisible" title="类目选择">
      <el-tree
        ref="tree"
        :data="categories"
        :props="defaultProps"
        node-key="id"
        default-expand-all
        :expand-on-click-node="false"
        style="width:60%"
      />
      <el-button type="primary" @click="chooseCategory">保存</el-button>
      <el-button @click="categoryDialogVisible = false">取消</el-button>
    </el-dialog>
  </div>
</template>

<script>
import {
  fetchContentCategories,
  fetchAllContent,
  addContent,
  updateContent,
  deleteContent,
  fetchContentByCategoryId
} from '@/api/cms'
export default {
  name: 'Content',
  data() {
    return {
      categories: [],
      contents: [], // 内容数据列表
      content: { pic: '', pic2: '' }, // 内容数据对象
      defaultProps: {
        children: 'children',
        label: 'label',
        isLeaf: 'isLeaf'
      },
      dialogVisible: false, // 内容编辑弹框的标识
      categoryDialogVisible: false, // 选择分类弹出框
      previewDialogVisible: false, // 图片预览弹窗标识
      dialogImageUrl: '', // 当前预览url
      limit: 1,
      cname: ''
    }
  },
  created: function() {
    this.getData()
  },
  methods: {
    // 获取分类和内容数据
    getData() {
      fetchContentCategories().then(res => {
        this.categories = res.data
      })
      fetchAllContent().then(res => {
        this.contents = res.data
      })
    },
    // 删除内容
    deleteClick(row) {
      // console.log("this.content===>" + JSON.stringify(row));
      this.$confirm('是否确认删除该内容数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 执行删除操作
          deleteContent(row.id).then(res => {
            this.getData()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 编辑内容
    handleClick(row) {
      this.dialogVisible = true
      console.log(row)
      this.content = {
        id: row.id,
        title: row.title,
        categoryId: row.categoryId,
        subTitle: row.subTitle,
        titleDesc: row.titleDesc,
        url: row.url,
        pic: row.pic,
        pic2: row.pic2,
        content: row.content
      }
    },
    // 添加内容数据
    add() {
      this.content = {
        id: '',
        title: '',
        subTitle: '',
        titleDesc: '',
        categoryId: '',
        url: '',
        pic: '',
        pic2: '',
        content: ''
      }
      this.dialogVisible = true
    },
    // 上传成功回调
    handleSuccess(res) {
      this.content.pic = res.data
      // console.log("this.content.pic == >" + this.content.pic);
    },
    handleRemove() {
      this.content.pic = ''
      // console.log("this.content.pic == >" + this.content.pic);
    },
    handlePreview(file) {
      // console.log(file);
      // 显示预览窗口
      this.previewDialogVisible = true
      // 显示预览图片
      this.dialogImageUrl = file.response.data
    },
    handleSuccess2(res) {
      this.content.pic2 = res.data
      // console.log("this.content.pic2 == >" + this.content.pic2);
    },
    handleRemove2() {
      // //从this.fileList中移除当前删除的file（file.response.data）即可
      // let i = this.content.pic2.indexOf(file.response.data);
      // if (i != -1) {
      //   this.item.image.splice(i, 1);
      // }
      this.content.pic2 = ''
      // console.log("content.pic2 == >" + this.content.pic2);
    },
    handlePreview2(file) {
      console.log(file)
      // 显示预览窗口
      this.previewDialogVisible = true
      // 显示预览图片
      this.dialogImageUrl = file.response.data
    },
    handleExceed() {
      this.$message({
        type: 'warning',
        message: '图片最大上传数为1'
      })
    },
    onSubmit() {
      // console.log("current content ==>" + JSON.stringify(this.content));
      // 判断新增还是更新
      if (this.content.id == '') {
        // 新增
        addContent(this.content).then(res => {
          this.$message({
            type: 'success',
            message: '新增成功'
          })
          this.getData()
          this.dialogVisible = false
        })
      } else {
        // 更新
        updateContent(this.content).then(res => {
          this.$message({
            type: 'success',
            message: '更新成功'
          })
          this.getData()
          this.dialogVisible = false
        })
      }
    },
    chooseCategory() {
      // 类目选择确定
      // 确认是否已经选择类目
      // this.$refs.tree.getCurrentNode() 获取当前选中节点{"id":837,"label":"太阳镜","isLeaf":true}
      // console.log(this.$refs.tree);
      if (this.$refs.tree.getCurrentNode() == null) {
        this.$message({
          type: 'warning',
          message: '请选择商品分类'
        })
        return
      }
      if (this.$refs.tree.getCurrentNode().isLeaf == false) {
        this.$message({
          type: 'warning',
          message: '请选择具体商品分类子分类'
        })
        return
      }
      this.content.categoryId = this.$refs.tree.getCurrentNode().id
      this.cname = this.$refs.tree.getCurrentNode().label
      this.categoryDialogVisible = false
    },
    nodeClick(data, node, array) {
      // console.log("data==>"+JSON.stringify(data));
      fetchContentByCategoryId(data.id).then(res => {
        this.contents = res.data
        this.$message({
          type: 'success',
          message: '内容显示成功'
        })
      })
    }
  }
}
</script>

<style>
</style>
